package com.vf.admin.utils;

import java.util.Random;

/**\
 * 随机数据
 */
public class RandomData {


    private static Random rand = new Random();

    public static <T> void swap(T[] a, int i, int j) {
        T temp = a[i];
        a[i] = a[j];
        a[j] = temp;
    }

    public static <T> void shuffle(T[] arr) {
        int length = arr.length;
        for (int i = length; i > 0; i--) {
            int randInd = rand.nextInt(i);
            swap(arr, randInd, i - 1);
        }
    }

    //时间复杂度O(n) n为数组长度
    //空间复杂度O(n) n为数组长度
    public static void rotate(Integer[] nums, int k) {
        k = k % nums.length;
        Integer[] tmp = nums.clone();
        System.arraycopy(tmp, tmp.length - k, nums, 0, k);
        System.arraycopy(tmp, 0, nums, k, tmp.length - k);
    }


    public static Integer[] playingCards(Integer[] arr){
        int lex=arr.length;
        //洗牌
        shuffle(arr);
        //切牌
        int randInd = rand.nextInt(lex);
        rotate(arr,randInd);
        return arr;
    }

    public static void main(String[] args) {
        Integer[] arr = new Integer[1000];
        for (int i = 0; i < 1000; i++) {
            arr[i] = (i + 1);
        }
        arr=playingCards(arr);
        for (Integer integer : arr) {
            System.out.println(integer);
        }
    }

}
